<?php

$events = array();

Yii::app()->clientScript->registerScriptFile(Html::jsUrl('coda.js'));

Yii::app()->clientScript->registerCssFile(Html::cssUrl('calendar.css'));

while(($row=$dataReader->read())!==false) {
			$events[intval($row['day'])] .= '<li><span class="title">'.stripslashes($row['title']).'</span><span class="desc">'.stripslashes($row['content']).'</span></li>';
		}

$today = date("Y/n/j", time());

$current_month = date("n", $time);

$current_year = date("Y", $time);

$current_month_text = date("F Y", $time);

$total_days_of_current_month = date("t", $time);

$first_day_of_month = mktime(0,0,0,$current_month,1,$current_year);

//geting Numeric representation for the first day of the month. 0 (for Sunday) through 6 (for Saturday).
$first_w_of_month = date("w", $first_day_of_month);

//calculate how many rows will be in the calendar to show the dates
$total_rows = ceil(($total_days_of_current_month + $first_w_of_month)/7);

//trick to show empty cell in the first row if the month doesn't start from Sunday
$day = -$first_w_of_month;

$next_month = mktime(0,0,0,$current_month+1,1,$current_year);
$next_month_text = date("F \'y", $next_month);

$previous_month = mktime(0,0,0,$current_month-1,1,$current_year);
$previous_month_text = date("F \'y", $previous_month);

$next_year = mktime(0,0,0,$current_month,1,$current_year+1);
$next_year_text = date("F \'y", $next_year);

$previous_year = mktime(0,0,0,$current_month,1,$current_year-1);
$previous_year_text = date("F \'y", $previous_year);

?>

<h2>Event List</h2>

<div class="action">
<a href="<?php echo Yii::app()->request->baseUrl; ?>/event/list" title="today"><?php echo Yii::t('yii','Today'); ?></a> | <?php echo Html::link(Yii::t('yii','New event'),array('create')); ?>  
</div>
<div class="calendar">
<h2><?=$current_month_text?></h2>
	<table cellspacing="0">
		<thead>
		<tr>
			<th><?php echo Yii::t('yii','Sun'); ?></th>
			<th><?php echo Yii::t('yii','Mon'); ?></th>
			<th><?php echo Yii::t('yii','Tue'); ?></th>
			<th><?php echo Yii::t('yii','Wed'); ?></th>
			<th><?php echo Yii::t('yii','Thu'); ?></th>
			<th><?php echo Yii::t('yii','Fri'); ?></th>
			<th><?php echo Yii::t('yii','Sat'); ?></th>
		</tr>
		</thead>
		<tr>
			<?php
			for($i=0; $i< $total_rows; $i++)
			{
				for($j=0; $j<7;$j++)
				{
					$day++;

					if($day>0 && $day<=$total_days_of_current_month)
					{
						//YYYY-MM-DD date format
						$date_form = "$current_year/$current_month/$day";

						echo '<td';

						//check if the date is today
						if($date_form == $today)
						{
							echo ' class="today"';
						}

						//check if any event stored for this date in $events array
						if(array_key_exists($day,$events))
						{
							//adding the date_has_event class to the <td> and close it
							echo ' class="date_has_event"> '.$day;

							//adding the eventTitle and eventContent wrapped with <span> and <li> to <ul>
							echo '<div class="events"><ul>'.$events[$day].'</ul></div>';
						}
						else
						{
							//if there is not event on that date then just close the <td> tag
							echo '> '.$day;
						}

						echo "</td>";
					}
					else
					{
						//showing empty cells in the first and last row
						echo '<td class="padding">&nbsp;</td>';
					}
				}
				echo "</tr><tr>";
			}

			?>
		</tr>
		<tfoot>
			<th>
				<a href="<?php echo Yii::app()->request->baseUrl; ?>/event/list/time/<?=$previous_year?>" title="<?=$previous_year_text?>">&laquo;&laquo;</a>
			</th>
			<th>
				<a href="<?php echo Yii::app()->request->baseUrl; ?>/event/list/time/<?=$previous_month?>" title="<?=$previous_month_text?>">&laquo;</a>
			</th>
			<th>&nbsp;</th>
			<th>&nbsp;</th>
			<th>&nbsp;</th>
			<th>
				<a href="<?php echo Yii::app()->request->baseUrl; ?>/event/list/time/<?=$next_month?>" title="<?=$next_month_text?>">&raquo;</a>
			</th>
			<th>
				<a href="<?php echo Yii::app()->request->baseUrl; ?>/event/list/time/<?=$next_year?>" title="<?=$next_year_text?>">&raquo;&raquo;</a>
			</th>
		</tfoot>
	</table>
</div>

<script type="text/javascript">
$.getScript('<?php echo Yii::app()->request->baseUrl; ?>/js/coda.js');  
</script>

